
import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import { loginAgain } from '@/utils/toThirdParty'

NProgress.configure({ showSpinner: false });

const whiteList = ['/auth', '/404'];

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  if (getToken()) {
  } else {
    if (to.path == '/' && to.query.authCode) {
      // 1.进入auth页面，进行授权登录操作---逻辑实现在auth文件下的的vue组件
      next(`/auth?authCode=${to.query.authCode}`)
      NProgress.done()
    }else if (whiteList.indexOf(to.path) !== -1) {    
      if(to.path === '/auth' && !to.query.authCode){
      // 2.重新进入到第三方网站获取授权码
      loginAgain()
      }else {
        // 进入白名单放行
        next()
      }
      NProgress.done()
    } else {
      // 没有token,不能访问权限页面，重新进入到第三方网站获取授权码
      loginAgain()
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})


